package com.cgg.stock.user.controller;

import com.cgg.stock.common.annotation.SkipToken;
import com.cgg.stock.common.pojo.User;
import com.cgg.stock.common.util.ResponseData;
import com.cgg.stock.common.util.ResponseDataUtil;
import com.cgg.stock.common.util.ResultEnums;
import com.cgg.stock.user.service.UserService;
import com.cgg.stock.user.vo.LoginBean;
import com.cgg.stock.user.vo.RegisterBean;
import com.cgg.stock.user.vo.dto.UserInfoDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
@Component
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;


    @GetMapping()
    public User Try(){
        return userService.getById(1);
    }


    /**
     * 用户注册
     * @param registerBean
     */
    @RequestMapping("register")

    @SkipToken
    public ResponseData register (@RequestBody RegisterBean registerBean){

        Integer re = userService.register(registerBean);
        if(re==200){
            return ResponseDataUtil.buildOk(ResultEnums.OK);
        }else {
            return ResponseDataUtil.buildError("该账号已注册");
        }

    }


    /**
     * 登录
     * @param loginBean
     * @return token
     * @throws Exception
     */
    @PostMapping("login")
    @SkipToken
    public ResponseData login(@RequestBody LoginBean loginBean) throws  Exception{
        Object token = userService.login(loginBean);
        //响应token, 用户名
        return ResponseDataUtil.buildOk(token);
    }

    @GetMapping("user/{userAccount}")
    public User queryUserByAccount(@PathVariable("userAccount") Long userAccount){
        User user = userService.queryUserByAccount(userAccount);
        return user;
    }


    @RequestMapping("/getUserInfo")
    public ResponseData getUserInfoForAccount(String account){
        UserInfoDTO userInfoDTO = userService.getUserInfo(account);

        return ResponseDataUtil.buildOk(1);

    }



    /**
     *  获得账户的余额
     * @param phone
     * @return
     */
    @RequestMapping("/getAccountBalance")
    public ResponseData getAccountBalance(Long phone){

        BigDecimal msg=userService.getAccountBalance(phone);

        return ResponseDataUtil.buildOk(msg);
    }


    /**
     * 改变账户余额
     * @param account
     * @param amount
     * @param count
     * @return
     */
    @PostMapping("/changeAccountBalance")
    public ResponseData ChangeAccountBalance(Long account, BigDecimal amount, int count){
        int msg=userService.updateAccountBalance(account,amount,count);
        return ResponseDataUtil.buildOk(msg);
    }


}
